package com.yike.user.service.impl;

import com.yike.common.exception.BusinessException;
import com.yike.common.tool.LoginUtil;
import com.yike.user.entity.VoiceSettings;
import com.yike.user.mapper.VoiceSettingsMapper;
import com.yike.user.param.VoiceSettingsParam;
import com.yike.user.service.VoiceSettingsService;
import com.yike.user.param.VoiceSettingsPageParam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yike.common.base.BaseServiceImpl;
import com.yike.common.pagination.Paging;
import com.yike.common.pagination.PageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yike.user.vo.VoiceSettingsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * 语音设置 服务实现类
 *
 * @author Sakura
 * @since 2025-04-02
 */
@Slf4j
@Service
public class VoiceSettingsServiceImpl extends BaseServiceImpl<VoiceSettingsMapper, VoiceSettings> implements VoiceSettingsService {

    @Autowired
    private VoiceSettingsMapper voiceSettingsMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveVoiceSettings(VoiceSettingsParam voiceSettingsParam) throws Exception {
        VoiceSettings voiceSettings = new VoiceSettings();
        BeanUtils.copyProperties(voiceSettingsParam, voiceSettings);
        voiceSettings.setCreateBy(LoginUtil.getUserId());
        return super.save(voiceSettings);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateVoiceSettings(VoiceSettingsParam voiceSettingsParam) throws Exception {
        VoiceSettings voiceSettings = voiceSettingsMapper.selectById(voiceSettingsParam.getId());
        if (voiceSettings == null) {
            throw new BusinessException("语音信息异常");
        }
        BeanUtils.copyProperties(voiceSettingsParam, voiceSettings);
        voiceSettings.setUpdateBy(LoginUtil.getUserId());
        voiceSettings.setUpdateTime(new Date());
        return super.updateById(voiceSettings);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteVoiceSettings(Long id) throws Exception {
        VoiceSettings voiceSettings = voiceSettingsMapper.selectById(id);
        if (voiceSettings == null) {
            throw new BusinessException("语音信息异常");
        }
        return super.removeById(id);
    }

    @Override
    public boolean batchDel(List<Long> ids) throws Exception {
        voiceSettingsMapper.deleteBatchIds(ids);
        return true;
    }

    @Override
    public VoiceSettingsVo getVoiceSettings(Long id) throws Exception {
        VoiceSettings voiceSettings = voiceSettingsMapper.selectById(id);
        if (voiceSettings == null) {
            throw new BusinessException("语音信息异常");
        }
        VoiceSettingsVo voiceSettingsVo = new VoiceSettingsVo();
        BeanUtils.copyProperties(voiceSettings, voiceSettingsVo);
        return voiceSettingsVo;
    }

    @Override
    public Paging<VoiceSettingsVo> getVoiceSettingsPageList(VoiceSettingsPageParam voiceSettingsPageParam) throws Exception {
        Page<VoiceSettings> page = new PageInfo<>(voiceSettingsPageParam);
        IPage<VoiceSettingsVo> iPage = voiceSettingsMapper.getVoiceSettingsList(page, voiceSettingsPageParam);
        return new Paging<VoiceSettingsVo>(iPage);
    }

    @Override
    public boolean sync() throws Exception {
        return true;
    }

}
